/**
 * 
 */
package com.zhiteng.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhiteng.entity.Item;
import com.zhiteng.entity.Order;
import com.zhiteng.entity.Teacher;
import com.zhiteng.service.ItemService;
import com.zhiteng.service.OrderService;
import com.zhiteng.service.TeacherService;
import com.zhiteng.utils.common.ZTResult;
import com.zhiteng.vo.OrderResultVo;

/**  
* <p>Title: OrderController.java</p>  
* <p>Description: 订单Controller</p>  
* @author dragon  
* @date 2018年12月27日  
* @version 1.0  
*/
@RequestMapping("/order")
@RestController
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private ItemService itemService;
	@Autowired
	private TeacherService teacherService;
	
	
	/**
	 * 
	* @Title: createOrder  
	* @Description: 下单：兑换积分礼品  
	* @param teacherId 教师id
	* @param itemId	商品id
	* @return ZTResult<Void>   
	* @throws
	 */
	@RequestMapping("/createOrder")
	public ZTResult<Object> createOrder(Order order) {
		if (StringUtils.isAnyBlank(order.getItemId()+"",order.getTeacherId())) {
			return ZTResult.result(1, "兑换失败，请检查参数");
		}   
		if(order.getItemId()==null) {
			return ZTResult.result(1, "兑换失败，请检查参数");
		}
		Item item = itemService.queryItemById(order.getItemId());
		
		//	保存订单
		int result = orderService.saveOrder(order);
		if(result==-1) return ZTResult.result(1, "兑换失败，未找到该积分礼品");
		if(result==-2) return ZTResult.result(1, "兑换失败，该积分礼品已兑换完");
		if(result==-3) return ZTResult.result(1, "兑换失败，未查询到该教师");
		if(result==-4) return ZTResult.result(1, "兑换该礼品积分不足");
		if(result==1) {
			Teacher teacher = teacherService.queryTeacherById(order.getTeacherId());
			OrderResultVo orderResultVo = new OrderResultVo();
			orderResultVo.setItemId(order.getItemId());
			orderResultVo.setPrice(item.getPrice());
			orderResultVo.setRemainingPoint(teacher.getBonusPoint());
			orderResultVo.setRule(item.getRule());
			return ZTResult.result(0, "兑换成功",orderResultVo);
		}
		return ZTResult.result(1, "兑换失败");
	}
	
	/**
	 * 
	* @Title: queryOrdersByTeacherId  
	* @Description: 根据教师id查询兑换记录（按订单创建时间降序排列）
	* @param teacherId
	* @return ZTResult<Object>   
	* @throws
	 */
	@RequestMapping("/queryOrders")
	public ZTResult<Object> queryOrdersByTeacherId(@RequestParam(name="teacherId")String teacherId) {
		List<Order> orderList = orderService.queryOrdersByTeacherId(teacherId);	//	按订单创建时间降序排列的
		return ZTResult.result(0, "查询成功",orderList);
	}
	
	/**
	 * 
	* @Title: queryOrdersByTeacherId  
	* @Description: 改变订单状态：0-未领取  1-已领取
	* @param status 订单状态：0-未领取  1-已领取
	* @param orderId 订单id
	* @return ZTResult<Object>   
	* @throws
	 */
	@RequestMapping("/changeOrderStauts")
	public ZTResult<Object> changeOrderStauts(@RequestParam(name="status")Integer status,
			@RequestParam(name="orderId")Long orderId) {
		int result = orderService.changeOrderStauts(orderId,status);
		if(result==1) {
			return ZTResult.result(0, "修改成功");
		}
		return ZTResult.result(1, "修改失败");
	}
	
}
